//
//  UIButton+InsetButton.m
//  CustomWindow
//
//  Created by heshun on 16/7/30.
//  Copyright © 2016年 heshun. All rights reserved.
//

#import "UIButton+InsetButton.h"

@implementation UIButton (InsetButton)

- (void)layoutButtonEdgeInsetStyle:(HSButtonEdgeInsetStyle)style imageTitleSpace:(CGFloat)space{
    
    CGFloat imageWidth = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    CGFloat lableWidth = 0.0;
    CGFloat lableHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue>=8.0) {
        lableWidth = self.titleLabel.intrinsicContentSize.width;
        lableHeight = self.titleLabel.intrinsicContentSize.height;
    }else{
        lableWidth = self.titleLabel.frame.size.width;
        lableHeight = self.titleLabel.frame.size.height;
    }
    
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets lableEdgeInsets = UIEdgeInsetsZero;
    switch (style) {
        case HSButtonEdgeInsetStyleTop:{
            
            imageEdgeInsets = UIEdgeInsetsMake(-lableHeight-space/2.0, 0, 0, -lableWidth);
            lableEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight-space/2.0, 0);
        }
            break;
        case HSButtonEdgeInsetStyleLeft:{
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            lableEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case HSButtonEdgeInsetStyleBottom:{
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -lableHeight-space/2.0, -lableWidth);
            lableEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWidth, 0, 0);
        }
            break;
        case HSButtonEdgeInsetStyleRight:{            
            imageEdgeInsets = UIEdgeInsetsMake(0, lableWidth+space/2.0, 0, -lableWidth-space/2.0);
            lableEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-space/2.0, 0, imageWidth+space/2.0);
        }
            break;
            
        default:
            break;
    }
    self.titleEdgeInsets = lableEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

@end
